/**
 * Created by Administrator on 2016/9/24.
 */
/**予执行函数**/
$(function(){
    var getShopCart = function(){
        var $shopcart = $.cookie("shopcart");

        if($shopcart != undefined){
            var json = strOper.get($shopcart);
            console.log(json);
            var html = "";
            $.each(json,function(i,o){
                html+=("   <tr>"+
                        " <td><a href = \"shopping.html\">"+
                " <img src = \""+ o.name+"\"/>"+
                    "</a>"+
                " </td>"+
                " <td>"+
                "<p>"+ o.img+"</p>"+
                "<span>"+ o.id+"</span>"+
                " </td>"+
                " <td>"+ o.price+"</td>"+
                " <td>"+
                " <div class = \"down\">-</div>"+
                " <div class = \"nume\">"+ o.count+"</div>"+
                " <div class = \"up\">+</div>"+
                " </td>"+
                " <td>" +
                "<i>"+o.price * o.count+"</i>" +
                "</td>"+
                " <td>" +
                "<div>删除</div>" +
                "</td>"+
                " </tr>");
            });
            $("#tby").html(html);

        }


    };
    var counter = function(element,type){
        var count = type === true ? 1 : -1;
        var $p = type === true ? $(element).prev() : $(element).next();
        if($p.html() == "1" && type === false){
            count = 0;
        }
        else{
            var $shopcart = $.cookie("shopcart");
            var id = $(element).data("pid");
            var result = strOper.counter($shopcart,id,type);
            $.cookie("shopcart",result);
        }
        $p.html(parseInt($p.html()) + count);
    };


    getShopCart();
});


